package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.Goods;
import com.qf.domain.GoodsType;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public List<GoodsType> select(Integer level) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_goods_type where level=?";
        try {
            return  qr.query(connection,sql,new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }

    @Override
    public long selectcount(String where, ArrayList<Object> list) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select count(*) from tb_goods"+where;
        try {
            return (long) qr.query(connection,sql,new ScalarHandler(),list.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }

    @Override
    public List<Goods> selectByde(int page_name, int page_size, String where, ArrayList<Object> list) {
        String sql="select * from tb_goods "+where+" order by id limit ?,?";
        Connection connection = DataSourceUtils.getConnection();

        try {
            list.add((page_name-1)*page_size);
            list.add(page_name*page_size);
//            String name =  list.get(1);
//            name=" % "+name+" % ";
//            list.set(1,name);
           return qr.query(connection,sql,new BeanListHandler<>(Goods.class),list.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }
    //根据id查询商品信息
    @Override
    public Goods selectgoods(int id) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_goods where id=?";
        try {
           return qr.query(connection,sql,new BeanHandler<>(Goods.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public GoodsType selectById(Integer typeid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_goods_type where id=?";
        try {
            return qr.query(connection,sql,new BeanHandler<>(GoodsType.class),typeid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }

    @Override
    public void addGoods(Goods goods) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_goods(id,name,pubdate,picture,price,star,intro,typeid)values(?,?,?,?,?,?,?,?)";
        Object[] params={goods.getId(),goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid()};
        try {
            qr.update(connection,sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }
}
